package com.qk.management.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.domain.customer.CustomerDO;
import com.qk.dto.customer.CustomerDTO;
import com.qk.dto.customer.UpdateCustomerInfoDTO;
import com.qk.entity.Customer;
import com.qk.management.mapper.CustomerMapper;
import com.qk.management.service.CustomerService;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author: byLengs
 * @Date: 2025/10/17 9:48
 * @Description:
 */
@Service
@SuppressWarnings("all")
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, Customer> implements CustomerService {
    @Override
    public void updateCustomerInfoById(UpdateCustomerInfoDTO dto) {
        Customer customer = BeanUtil.copyProperties(dto, Customer.class);
        customer.setUpdateTime(LocalDateTime.now());
        this.updateById(customer);
    }

    @Override
    public PageResult<CustomerDO> customerList(CustomerDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<CustomerDO> page = this.baseMapper.selectByPage(dto);
        return PageResult.<CustomerDO>builder()
                .total(page.getTotal())
                .rows(page.getResult())
                .build();
    }

    @Override
    public void addCustomer(Customer customer) {
        if (ObjectUtil.isNull(customer.getName()) || ObjectUtil.isNull(customer.getPhone())) {
            throw new RuntimeException("客户姓名和手机号不能为空");
        }
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());
        this.baseMapper.insert(customer);
    }
}
